﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_SEC_OBJECT
// * CreateDate    : 2011/7/15 14:32:14
// * Creater       : CodeSmith
// * LastChangeDate: 2011/7/15 14:32:14
// * LastChanger   : 2011/7/15 14:32:14
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.Business.Sec
{
    /// <summary>
    /// SecObject
    /// </summary>
    public partial class SecObjectBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objSecObjectEntity">Entity</param>
        /// <returns>Entity</returns>
        public static SecObjectEntity GetSecObject(SecObjectEntity objSecObjectEntity)
        {
            return DataAccess.SelectSingle<SecObjectEntity>(objSecObjectEntity);
        }
        public static SecObjectEntity GetSecObjectByID(string objectID)
        {
            if (string.IsNullOrEmpty(objectID))
            {
                return null;
            }
            SecObjectEntity entity = new SecObjectEntity();
            entity.ObjectId = objectID;
            return DataAccess.SelectSingle<SecObjectEntity>(entity);
        }
        public static List<SecObjectEntity> GetSecObjectByMenuID(string menuID)
        {
            if (string.IsNullOrEmpty(menuID))
            {
                return null;
            }
            SecObjectEntity entity = new SecObjectEntity();
            entity.MenuId = menuID;
            return DataAccess.Select<SecObjectEntity>(entity);
        }
        public static SecObjectEntity GetSecObject(string menuId, string objectCode)
        {
            if (string.IsNullOrEmpty(menuId) || string.IsNullOrEmpty(objectCode))
            {
                return null;
            }
            SecObjectEntity entity = new SecObjectEntity();
            entity.MenuId = menuId;
            entity.ObjectCode = objectCode;
            return DataAccess.SelectSingle<SecObjectEntity>(entity);
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objSecObjectEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertSecObject(SecObjectEntity objSecObjectEntity)
        {
            return DataAccess.Insert(objSecObjectEntity);
        }

        public static bool InsertSecObject(List<SecObjectEntity> objSecObjectEntityList)
        {
            return DataAccess.Insert(objSecObjectEntityList);
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objSecObjectEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateSecObject(SecObjectEntity objSecObjectEntity)
        {
            return DataAccess.Update(objSecObjectEntity);
        }

        public static bool UpdateSecObject(List<SecObjectEntity> objSecObjectEntityList)
        {
            return DataAccess.Update<SecObjectEntity>(objSecObjectEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objSecObjectEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteSecObject(SecObjectEntity objSecObjectEntity)
        {
            return DataAccess.Delete(objSecObjectEntity);
        }

        public static bool DeleteSecObject(List<SecObjectEntity> objSecObjectEntityList)
        {
            return DataAccess.Delete<SecObjectEntity>(objSecObjectEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetSecObjectAll()
        {
            return DataAccess.SelectAllDataSet<SecObjectEntity>();
        }
        #endregion

        #region Functions
        public static bool ImportObjects(DataTable dt)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    foreach (DataRow dr in dt.Rows)
                    {
                        string menuId = CommonFunctions.ToString(dr["MENU_ID"]);
                        string objectCode = CommonFunctions.ToString(dr["OBJECT_CODE"]);
                        SecObjectEntity entity = SecObjectBs.GetSecObject(menuId, objectCode);
                        if (entity == null)
                        {
                            entity = new SecObjectEntity();
                            entity.ObjectId = Guid.NewGuid().ToString();
                            entity.MenuId = Convert.ToString(CommonFunctions.ToObject(dr["MENU_ID"]));
                            entity.ObjectCode = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_CODE"]));
                            entity.ObjectName = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_NAME"]));
                            entity.ObjectRemark = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_REMARK"]));
                            DataAccess.Insert(entity, broker);
                        }
                        else
                        {
                            entity.ObjectName = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_NAME"]));
                            entity.ObjectRemark = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_REMARK"]));
                            DataAccess.Update(entity, broker);
                        }
                    }

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        public static bool ImportObjects(DataRow dr)
        {
            try
            {
                string menuId = CommonFunctions.ToString(dr["MENU_ID"]);
                string objectCode = CommonFunctions.ToString(dr["OBJECT_CODE"]);
                SecObjectEntity entity = SecObjectBs.GetSecObject(menuId, objectCode);
                if (entity == null)
                {
                    entity = new SecObjectEntity();
                    entity.ObjectId = Guid.NewGuid().ToString();
                    entity.MenuId = Convert.ToString(CommonFunctions.ToObject(dr["MENU_ID"]));
                    entity.ObjectCode = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_CODE"]));
                    entity.ObjectName = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_NAME"]));
                    entity.ObjectRemark = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_REMARK"]));
                    DataAccess.Insert(entity);
                }
                else
                {
                    entity.ObjectName = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_NAME"]));
                    entity.ObjectRemark = Convert.ToString(CommonFunctions.ToObject(dr["OBJECT_REMARK"]));
                    DataAccess.Update(entity);
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }


}

